package com.material.demo.record;

import android.media.AudioRecord;
import android.media.AudioTrack;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WenJackp on 2017/2/20.
 */

public class BaseRecordHelper {

    //自定义的最小Buffer
    protected static final int MIN_BUFFER_SIZE = 1024;
    //据说适用所有的设备
    protected static final int DEFAULT_SAMPLE_RATE = 441000;
    //无效值
    protected static final int INVALID_VALUE = -1;
    //android设备可能支持的sampleRate
    protected final int[] DEFAULT_SUPPORT_SAMPLE_RATE = new int[]{8000, 11025, 16000, 22050, 44100, 48000};

    protected List<Integer> mSupportRates;

    /**
     * 获取最小的BufferSize
     *
     * @param sampleRateInHz   采样率这个值和设备有关通常包含 8000 11025 16000 44100;
     * @param channelConfig    Audio Channel AudioFormat.CHANNEL_IN_MONO(低音质)  AudioFormat.CHANNEL_IN_STEREO(高音质)  区分输入和输出 IN 和 OUT
     * @param audioFormat      @See{AudioFormat.ENCODING_?}
     * @param defaultMinBuffer
     * @return
     */
    protected int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat, int defaultMinBuffer) {
        int minBufferSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfig, audioFormat);
        if (defaultMinBuffer > 0) {
            if (minBufferSize == AudioTrack.ERROR || minBufferSize == AudioTrack.ERROR_BAD_VALUE) {
                minBufferSize = defaultMinBuffer;
            }
        }
        return minBufferSize;
    }


    /**
     * 获取最合适的采样比
     *
     * @param channelConfig
     * @param audioFormat
     * @param sampleRateInHz 指定目标采样率
     * @param notFindValue   找不到合适采样率的时候返回值
     * @return
     */
    protected int getBestSimpleRate(int channelConfig, int audioFormat, int sampleRateInHz, int notFindValue) {

        if (mSupportRates == null) {
            checkSupportSampleRate(channelConfig, audioFormat);
        }

        if (mSupportRates != null && mSupportRates.size() > 0) {
            int bestRate = 0;
            for (int i = 0, j = mSupportRates.size(); i < j; i++) {
                int rate = mSupportRates.get(i);
                if (rate <= sampleRateInHz) {
                    bestRate = rate;
                } else {
                    break;
                }
            }
            return bestRate;
        }
        return notFindValue;
    }

    /**
     * 检测设备支持哪些常用的SampleRate
     *
     * @param channelConfig
     * @param audioFormat
     */
    protected void checkSupportSampleRate(int channelConfig, int audioFormat) {
        for (int i = 0, j = DEFAULT_SUPPORT_SAMPLE_RATE.length; i < j; i++) {
            int rate = DEFAULT_SUPPORT_SAMPLE_RATE[i];
            int minBuffer = getMinBufferSize(rate, channelConfig, audioFormat, INVALID_VALUE);
            if (minBuffer > 0) {
                if (mSupportRates == null) {
                    mSupportRates = new ArrayList<>();
                }
                mSupportRates.add(rate);
            }
        }
        logMsg("checkSupportSampleRate : SupportSize : " + (mSupportRates == null ? 0 : mSupportRates.size()));
    }

    protected void logMsg(String msg) {
        Log
                .d(getTag(), msg);
    }

    protected String getTag() {
        return BaseRecordHelper.class.getName();
    }
}
